﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto.Fixup;
using AssetsSys.Dto.Permission;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Auth;
using AssetsSys.WebApi.Models.Fixup;
using AssetsSys.WebApi.Models.Permission;
using AssetsSys.WebApi.Models.Source;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 维修记录
    /// </summary>
    [MyAuth]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/fixuplog")]
    public class FixupController : ApiController
    {

        /// <summary>
        /// 获取我的报修记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("mylist")]
        public async Task<IHttpActionResult> GetList([FromUri]FixupQuertDto request)
        {
            var userId = ((UserIdentity)User.Identity).Id;
            var data = await FixupManger.GetList(request, userId);
            return this.SendData(data);
        }
        /// <summary>
        /// 获取维修记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public async Task<IHttpActionResult> GetFixupList([FromUri]FixupQuertDto request)
        {
            var userId = ((UserIdentity)User.Identity).Id;
            var data = await FixupManger.GetFixupList(request, userId);
            return this.SendData(data);
        }
        /// <summary>
        /// 添加维修记录
        /// </summary>
        /// <param name="assetsId">资产Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("{assetsid}/infos")]
        public async Task<IHttpActionResult> CreateFixup(Guid assetsId,CreateFixlogViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            model.AssetsId = assetsId;
            var repairId = ((UserIdentity)User.Identity).Id;
            return await FixupManger.AddFixup(model.AssetsId,repairId,model.FxiupDescribe) ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }

        /// <summary>
        /// 完成维修
        /// </summary>
        /// <param name="fixorderid">维修信息Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{fixorderid}/infos")]
        public async Task<IHttpActionResult> UpdateFixup(Guid fixorderid, CreateFixlogViewModel model)
        {
            var fixId = ((UserIdentity)User.Identity).Id;
            return await FixupManger.Update(fixorderid, fixId, model.NewState, model.FxiupDescribe) ? this.SendData("修改成功") : this.ErrorData("修改失败"); ;
        }

    }
}
